<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%!
    public Map cate(List<Map> list, Map dest, List<Map> _dest) {
        List<Map> subs = (List<Map>)dest.get("subs");
        if (subs == null) {
          subs = new ArrayList<Map>();
          dest.put("subs", subs);
        }
        String _parent = Tools.getDesc(dest.get("server_id")+"", "-1001");
        Iterator<Map> iterator = list.iterator();
        while (iterator.hasNext()) {
          Map map = iterator.next();
          String parent = Tools.getDesc(map.get("parent_id")+"", "-1000");
          if (_parent.equals(parent)) {
            iterator.remove();
            _dest.add(map);
            List<Map> _list = new ArrayList<Map>();
            _list.addAll(list);
            subs.add(cate(_list, map, _dest));
          }
        }
        dest.put("size", subs.size() + "");
        return dest;
    }
%>
<%
    String branch = input.getDesc("branch", "");
    String parent = input.getDesc("parent", "-1");
    Where where = new Where();
    if (!"".equals(branch)) {
        where.and("branch=", branch);
    }
    List<Map> services = QBuilder.db("basedb").select("new map(id as server_id, name as name, parentId as parent_id)").from("Service").where(where.and("hospital=", hospital).and("status=", STATUS_ENABLE)).query().getResultObj();
    if (services.size() == 0) {
        msg = new Message(30041, "没有相关数据");
        msg.put("data", new ArrayList());
    } else {
        List<Map> dest = new ArrayList<Map>();
        List<Map> _dest = new ArrayList<Map>();
        Iterator<Map> iterator = services.iterator();
        while (iterator.hasNext()) {
          Map server = iterator.next();
          server.put("size", "0");
          server.put("subs", new ArrayList<Map>());
          String _parent = Tools.getDesc(server.get("parent_id")+"", "-1");
          if (parent.equals(_parent)) {
            iterator.remove();
            _dest.add(server);
            List<Map> _list = new ArrayList<Map>();
            _list.addAll(services);
            dest.add(cate(_list, server, _dest));
          }
        }
        services.removeAll(_dest);
        dest.addAll(services);
        Map resultMap = new HashMap();
        resultMap.put("size", dest.size());
        resultMap.put("servers", dest);
        msg.put("data", resultMap);
    }
    Tools.encryptPrint(out, msg.toJson());
%>
